<?php

namespace App\Http\Controllers\Api;

use App\Basic;
use App\Education;
use App\Flaunt;
use App\Http\Requests\EducationRequest;
use App\Http\Requests\FlauntRequest;
use App\Http\Requests\IntentionResquest;
use App\Http\Requests\LanguageRequest;
use App\Http\Requests\ProjectRequest;
use App\Http\Requests\StoreBasic;
use App\Http\Requests\UpdateResumeBasic;
use App\Http\Requests\WorkRequest;
use App\Intention;
use App\Language;
use App\Project;
use App\UserPersonnel;
use App\Work;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

//我的简历
class ResumeController extends Controller
{
    /**
     * @return mixed
     * 获取我的简历
     */
    public function index()
    {
        $info = UserPersonnel::where('user_id', Auth::id())->with(['user', 'basic', 'work', 'education', 'intention', 'language', 'project', 'flaunt'])->first();
        //个人图片转数组
        if ($info->flaunt) {
            if ($info->flaunt->image) {
                $info->flaunt->image = explode(',', $info->flaunt->image);
            }
        }
        return $info;
    }

    /**
     * 刷新简历
     */
    public function refreshResume()
    {
        UserPersonnel::where('user_id', Auth::id())->update(['updated_at' => date('Y-m-d H:i:s')]);
        return response(['status' => 1, 'message' => '刷新成功']);
    }

    /**
     * @param StoreBasic $request
     * @param Basic $basic
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历基本信息模块
     */
    public function storeBasic(StoreBasic $request, Basic $basic)
    {
        if ($basic::where('pid', $request->pid)->first()) {
            return response(['status' => 0, 'message' => '创建失败', 'data' => []], 400);
        }
        $user = UserPersonnel::where('user_id', Auth::id())->first();
        $data = [];
        $data['uid'] = $user->user_id;
        $data['pid'] = $user->id;
        $data['company'] = $request->company;
        $data['true_name'] = $request->true_name;
        $data['true_name_look'] = $request->true_name_look;
        $data['mobile'] = $request->mobile;
        $data['mailbox'] = $request->mailbox;
        $data['birth'] = $request->birth;
        $data['province'] = $request->province;
        $data['city'] = $request->city;
        $data['area'] = $request->area;
        $data['start_work_time'] = $request->start_work_time;
        $data['annual_salary'] = $request->annual_salary;
        $data['is_look_annual_salary'] = $request->is_look_annual_salary;
        $data['now_status'] = $request->now_status;
        $res = $basic::create($data);
        if ($res) {
            return response(['message' => '创建成功']);
        }
        return response(['message' => '创建失败'], 400);
    }

    /**
     * @param StoreBasic $request
     * @param Basic $basic
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历基本信息模块
     */
    public function updateBasic(StoreBasic $request, Basic $basic)
    {
        if ($basic::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
        }
        if ($basic::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['status' => 1, 'message' => '修改成功', 'data' => $basic::where('id', $request->id)->first()]);
        }
        return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
    }

    /**
     * @param WorkRequest $request
     * @param Work $work
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历工作经历模块
     */
    public function storeWork(WorkRequest $request, Work $work)
    {
        $user = UserPersonnel::where('user_id', Auth::id())->first();
        $res = $work::create(array_merge($request->all(), ['uid' => $user->user_id, 'pid' => $user->id]));
        if ($res) {
            return response(['message' => '创建成功']);
        }
        return response(['message' => '创建失败']);
    }

    /**
     * @param WorkRequest $request
     * @param Work $work
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历工作经历模块
     */
    public function updateWork(WorkRequest $request, Work $work)
    {
        if ($work::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['message' => '修改失败'], 400);
        }
        if ($work::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['message' => '修改成功']);
        }
        return response(['message' => '修改失败'], 400);
    }

    /**
     * @param EducationRequest $request
     * @param Education $education
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历教育背景模块
     */
    public function storeEducation(EducationRequest $request, Education $education)
    {
        $user = UserPersonnel::where('user_id', Auth::id())->first();
        $res = $education::create(array_merge($request->all(), ['uid' => $user->user_id, 'pid' => $user->id]));
        if ($res) {
            return response(['message' => '创建成功']);
        }
        return response(['message' => '创建失败']);
    }

    /**
     * @param EducationRequest $request
     * @param Education $education
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历教育背景模块
     */
    public function updateEducation(EducationRequest $request, Education $education)
    {
        if ($education::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['message' => '修改失败'], 400);
        }
        if ($education::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['message' => '修改成功']);
        }
        return response(['message' => '修改失败'], 400);
    }

    /**
     * @param IntentionResquest $request
     * @param Intention $intention
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历求职意向模块
     */
    public function storeIntention(IntentionResquest $request, Intention $intention)
    {
        if ($intention::where('pid', $request->pid)->first()) {
            return response(['status' => 0, 'message' => '创建失败', 'data' => []], 400);
        }
        $res = $intention::create(array_merge($request->all(), ['uid' => Auth::id()]));
        if ($res) {
            return response(['status' => 1, 'message' => '创建成功', 'data' => $res]);
        }
        return response(['status' => 0, 'message' => '创建失败', 'data' => []]);
    }

    /**
     * @param IntentionResquest $request
     * @param Intention $intention
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历求职意向模块
     */
    public function updateIntention(IntentionResquest $request, Intention $intention)
    {
        if ($intention::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
        }
        if ($intention::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['status' => 1, 'message' => '修改成功', 'data' => $intention::where('id', $request->id)->first()]);
        }
        return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
    }

    /**
     * @param LanguageRequest $request
     * @param Language $language
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历语言能力模块
     */
    public function storeLanguage(LanguageRequest $request, Language $language)
    {
        if ($language::where('pid', $request->pid)->first()) {
            return response(['status' => 0, 'message' => '创建失败', 'data' => []], 400);
        }
        $res = $language::create(array_merge($request->all(), ['uid' => Auth::id()]));
        if ($res) {
            return response(['status' => 1, 'message' => '创建成功', 'data' => $res]);
        }
        return response(['status' => 0, 'message' => '创建失败', 'data' => []]);
    }

    /**
     * @param LanguageRequest $request
     * @param Language $language
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历语言能力模块
     */
    public function updateLanguage(LanguageRequest $request, Language $language)
    {
        if ($language::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
        }
        if ($language::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['status' => 1, 'message' => '修改成功', 'data' => $language::where('id', $request->id)->first()]);
        }
        return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
    }

    /**
     * @param ProjectRequest $request
     * @param Project $project
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建个人简历项目经验模块
     */
    public function storeProject(ProjectRequest $request, Project $project)
    {
        $res = $project::create(array_merge($request->all(), ['uid' => Auth::id()]));
        if ($res) {
            return response(['status' => 1, 'message' => '创建成功', 'data' => $res]);
        }
        return response(['status' => 0, 'message' => '创建失败', 'data' => []]);
    }

    /**
     * @param ProjectRequest $request
     * @param Project $project
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 修改个人简历项目经验模块
     */
    public function updateProject(ProjectRequest $request, Project $project)
    {
        if ($project::where('id', $request->id)->value('uid') != Auth::id()) {
            return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
        }
        if ($project::where('id', $request->id)->update($request->except(['id', 'pid', 'uid']))) {
            return response(['status' => 1, 'message' => '修改成功', 'data' => $project::where('id', $request->id)->first()]);
        }
        return response(['status' => 0, 'message' => '修改失败', 'data' => []], 400);
    }

    /**
     * @param FlauntRequest $request
     * @param Flaunt $flaunt
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
     * 创建修改个人简历个人图片，视频展示
     */
    public function flaunt(FlauntRequest $request, Flaunt $flaunt)
    {
        $user_id = Auth::id();
        $image = $request->image;
        if ($image) {
            $image = implode(',', $image);
        }
        if (strlen($image) > 1000) {
            return response(['message' => '图片过多'], 400);
        }
        $video = $request->video;
        if (strlen($video) > 191) {
            return response(['message' => '视频错误'], 400);
        }
        $flaunts = $flaunt->where('user_id', $user_id)->first();
        if ($flaunts) {//修改
            if (!$image || !$video) {
                return response(['message' => '修改成功']);
            }
            if ($image) {
                $flaunts->image = $image;
            }
            if ($video) {
                $flaunts->video = $video;
            }
            $flaunts->save();
            return response(['message' => '修改成功']);
        } else {//创建
            $data = [];
            $data['user_id'] = $user_id;
            if ($image) {
                $data['image'] = $image;
            }
            if ($video) {
                $data['video'] = $video;
            }
            $res = $flaunt->create($data);
            if ($res) {
                return response(['message' => '上传成功']);
            }
            return response(['message' => '上传失败'], 400);
        }
    }

    //图片或视频删除
    public function flauntDeletes(Request $request)
    {
        $type = $request->type;//image  video
        $info = Flaunt::where('user_id', Auth::id())->first();
        if ($type == 'image') {
            $info->image = '';
        } elseif ($type == 'video') {
            $info->video = '';
        } else {
            return response(['message' => '删除失败'], 400);
        }
        $info->save();
        return response(['message' => '删除成功']);
    }

    public function flauntDelete(Request $request)
    {
        $user_id = Auth::id();
        $type = $request->type;//image  video
        $path = $request->path;
        $info = Flaunt::where('user_id', $user_id)->first();
        if ($type == 'image') {
            $data = explode(',', $info->image);
            foreach ($data as $k => $v) {
                if ($v == $path) {
                    unset($data[$k]);
                    //只是删除了数据库，图片并未删除
                }
            }
            if ($data) {
                $data = implode(',', $data);
            } else {
                $data = null;
            }
            $info->image = $data;
            $res = $info->save();
            if ($res) {
                return response(['message' => '删除成功']);
            }
            return response(['message' => '删除失败'], 400);
        } elseif ($type == 'video') {
            $info->video = null;
            $res = $info->save();
            //只是删除了数据库，视频并未删除
            if ($res) {
                return response(['message' => '删除成功']);
            }
            return response(['message' => '删除失败'], 400);

        } else {
            return response(['message' => '删除失败'], 400);
        }
    }

}
